﻿using Business.Service;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcAppWeb.Controllers
{
    public class PickBatchController : BaseDataController
    {
        //
        // GET: /ActionType/

        public ActionResult PickBatch()
        {
            if (Session["UserInfo"] == null) return RedirectToAction("LogOn", "Account");
            else
            {
                AppUserList model = (AppUserList)Session["UserInfo"];
                return View(model);

            }
        }

        ///<summary>
        ///初始化数据
        ///</summary>
        ///<returns></returns>
        public JsonResult InitJsonResult(string batchNumber, string checkTimeS, string checkTimeE)
        {
            int page = int.Parse(Request["page"].ToString());
            int rows = int.Parse(Request["rows"].ToString());
            int total = 0;
            string where = " where 1=1";
            if (!string.IsNullOrEmpty(batchNumber))
            {
                where += @" and t1.BATCHNUMBER like'%" + batchNumber + "%'";
            }
            if (!string.IsNullOrEmpty(checkTimeS))
            {
                where += " and t1.BATCHDATE>= '" + checkTimeS + "'";
            }
            if (!string.IsNullOrEmpty(checkTimeE))
            {
                where += " and t1.BATCHDATE<= '" + checkTimeE + "'";
            }
            where += " order by t1.BATCHDATE ";
            var retData = new object();
            BPickBatch db = new BPickBatch();
            retData = db.GetPickBatchInfoList(page, rows, out total, where).ToList();
            Dictionary<string, object> json = new Dictionary<string, object>();
            json.Add("total", total);
            json.Add("rows", retData);
            return Json(json);
        }

        [HttpPost]
        public string SaveData()
        {
            string sjson = string.Empty;
            string batchNumber = Request["hiddenBatchNumber"];
            string batchDate = Request["hiddenBatchDate"];
            int flag = Convert.ToInt32(Request["hiddenFlag"]);
            AppUserList model = (AppUserList)Session["UserInfo"];

            BPickBatch np = new BPickBatch();
            int n = np.UpdatePickBatchInfo(batchNumber, batchDate, flag, model.USERNAME, DateTime.Now);
            if (n > 0)
            {
                sjson = "{success:true}";
            }
            else
            {
                sjson = "{success:false,msg:'添加失败.请联系管理员！'}";
            }
            return sjson;
        }

    }
}
